package com.app.shop.fragments;


import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.app.shop.R;
import com.app.shop.activitys.GoodsListActivity;
import com.app.shop.activitys.LoginActivity;
import com.app.shop.activitys.MyInfoActivity;
import com.app.shop.activitys.OrderListActivity;
import com.app.shop.activitys.PassWordResetActivity;
import com.app.library.base.BaseFragment;
import com.app.library.beans.EventMessage;
import com.app.library.beans.UserBean;
import com.app.library.utils.DialogUtil;
import com.app.library.utils.SharedPreferencesUtil;
import com.app.library.utils.StringUtil;
import com.app.library.utils.UserManager;
import com.app.library.widgts.CustomDialog;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment {


    ImageView imgvFace;
    TextView tvName;
    private String dialog_title = "退出登录";
    private String dialog_content = "是否确定退出登录？";
    private CustomDialog customDialog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_more, container, false);
        imgvFace = view.findViewById(R.id.imgv_face);
        tvName = view.findViewById(R.id.tv_name);
        RelativeLayout relamanage = view.findViewById(R.id.rela_manage);
        RelativeLayout relaorder = view.findViewById(R.id.rela_order);
        RelativeLayout relapwd = view.findViewById(R.id.rela_pwd);
        RelativeLayout relamyinfo = view.findViewById(R.id.rela_my_info);
        RelativeLayout relalogout = view.findViewById(R.id.rela_logout);
        relamanage.setOnClickListener(this::onViewClicked);
        relaorder.setOnClickListener(this::onViewClicked);
        relapwd.setOnClickListener(this::onViewClicked);
        relamyinfo.setOnClickListener(this::onViewClicked);
        relalogout.setOnClickListener(this::onViewClicked);
        registerEventBus();
        setView();
        return view;
    }
    public static MineFragment getInstance(){
        MineFragment fragment=new MineFragment();
        return fragment;
    }
    private void setView() {

        UserBean bean = UserManager.getUser(UserManager.getUserId(getActivity()));
        tvName.setText(StringUtil.getContentEmpty(bean.name));
    }



    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rela_pwd:
                skipActivity(getActivity(), PassWordResetActivity.class);

                break;
            case R.id.rela_order:
                skipActivity(getActivity(), OrderListActivity.class);
                break;

            case R.id.rela_manage:
                skipActivity(getActivity(), GoodsListActivity.class);
                break;

            case R.id.rela_my_info:
                skipActivity(getActivity(), MyInfoActivity.class);
                break;

            case R.id.rela_logout:
                Logout();
                break;
        }
    }

    private void Logout() {
        customDialog = DialogUtil.showDialog(getActivity(), customDialog, 2, dialog_title, dialog_content, "取消", "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                SharedPreferencesUtil.removeAll(getActivity(), "user");
                skipActivity(getActivity(), LoginActivity.class);
                getActivity().finish();
            }
        });

        if (customDialog != null && !customDialog.isShowing()) {
            customDialog.show();
        }


        customDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                customDialog = null;
            }
        });
    }

    @Override
    public void onEvent(EventMessage msg) {
        super.onEvent(msg);
        if (msg.getMessageType() == EventMessage.REFRESH) {
            setView();
        }
    }
}
